package com.beiing.tianshuai.tianshuai.model;

import com.beiing.tianshuai.tianshuai.entity.AddFriendsBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Jinni on 2017/8/8 0008.
 */

public class AddFriendsModel implements AddFriendsModelImpl{

    private static final String                     TAG = "AddFriendsModel";
    private AddFriendsModel.OnRequestListener       mListener;

    public AddFriendsModel(OnRequestListener listener) {
        mListener = listener;
    }

    @Override
    public void getAddFriendsResult(String fromId, String toId, String message) {
        Observable<AddFriendsBean> loginResult = DataManager.getAddFriendResult(
                fromId,
                toId,
                message
        );
        Observable<AddFriendsBean> signUpBeanObservable = loginResult.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<AddFriendsBean>() {

            @Override
            public void onNext(@NonNull AddFriendsBean bean) {
                if (mListener != null) {
                    mListener.onSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    public interface OnRequestListener {
        void onSuccess(AddFriendsBean bean);
        void onError(Throwable throwable);
    }
}
